﻿<Serializable()> Public Module ModifyDEM
    ''' <summary>
    ''' 由于东半球的数据使用时储存问题，因此要进行修改。修改为区域-30。如果结果为
    ''' </summary>
    ''' <param name="sFileName"></param>
    ''' <remarks></remarks>
    Public Function ModifyUtmDEM(ByVal sFileName As String) As Boolean
        Try


            Dim LA As String = ""
            FileOpen(1, sFileName, OpenMode.Input)
            LA = LineInput(1)
            FileClose(1)

            Dim C(167) As Char
            For i As Integer = 0 To 167
                C(i) = LA(i)
            Next
            If CInt(C(166).ToString) >= 3 Then
                C(166) = CChar((CInt(C(166).ToString) - 3).ToString)
            End If
            FileOpen(1, sFileName, OpenMode.Binary)
            For i As Integer = 0 To 167
                Dim achart As Char = C(i)
                FilePut(1, achart)
            Next
            FileClose(1)
        Catch ex As Exception
            MessageBox.Show("DEM文件不存在或格式错误!", "DEM文件错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
        Return True
    End Function

    ''' <summary>
    ''' 由于东半球的数据使用时储存问题，因此要进行修改。纬度坐标
    ''' </summary>
    ''' <param name="sFileName"></param>
    ''' <remarks></remarks>
    Public Function ModifyGeoDEM(ByVal sFileName As String) As Boolean
        Try

            Dim strAll As String = My.Computer.FileSystem.ReadAllText(sFileName)

            Dim strA(-1) As String
            Dim LA As String = strA(0) '读取第一行，并进行处理

            Dim strLongSW As String = LA.Substring(546, 24) '西南角的经度
            Dim strLongNW As String = LA.Substring(594, 24) '西北角的经度
            Dim strLongNE As String = LA.Substring(642, 24) '东北角的经度
            Dim strLongSE As String = LA.Substring(690, 24) '东北角的经度
            strLongSW = strLongSW.Replace("D", "E")
            strLongNW = strLongNW.Replace("D", "E")
            strLongNE = strLongNE.Replace("D", "E")
            strLongSE = strLongSE.Replace("D", "E")

            strLongSW = (CDbl(strLongSW) - 180 * 3600).ToString.PadLeft(24)
            strLongNW = (CDbl(strLongNW) - 180 * 3600).ToString.PadLeft(24)
            strLongNE = (CDbl(strLongNE) - 180 * 3600).ToString.PadLeft(24)
            strLongSE = (CDbl(strLongSE) - 180 * 3600).ToString.PadLeft(24)

            LA = LA.Remove(546, 24)
            LA = LA.Insert(546, strLongSW)
            LA = LA.Remove(594, 24)
            LA = LA.Insert(594, strLongNW)
            LA = LA.Remove(642, 24)
            LA = LA.Insert(642, strLongNE)
            LA = LA.Remove(690, 24)
            LA = LA.Insert(690, strLongSE)
            strA(0) = LA
            For i As Integer = 1 To strA.Length - 1
                LA = strA(i) '读取每一行，并进行处理
                Dim strL As String = LA.Substring(17, 24) '西南角的经度
                strL = strL.Replace("D", "E")
                strL = (CDbl(strL) - 180 * 3600).ToString.PadLeft(24)
                LA = LA.Remove(17, 24)
                LA = LA.Insert(17, strLongSW)
                strA(i) = LA
            Next
            FileOpen(1, sFileName, OpenMode.Output)
            For i As Integer = 0 To strA.Length - 1
                WriteLine(1, strA(i))
            Next
            FileClose(1)
        Catch ex As Exception
            MessageBox.Show("DEM文件不存在或格式错误!", "DEM文件错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
        Return True
    End Function
End Module
